<template>
  <div class="main-container">
    <div style="box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04)">
      <div>
        <el-input prefix-icon="el-icon-search" type="text" style="width:200px" v-model="findName"
          placeholder="输入客户姓名..." clearable></el-input>
        <el-button style="margin:0 30px" type="success" @click="findByName">查询</el-button>
        <!-- 弹出框 -->
        <el-button type="primary" @click="dialogVisible = true"> 办理退住</el-button>
        <el-dialog title="办理退住" :visible.sync="dialogVisible" width="30%" v-if="dialogVisible" @close='closeDialog'>
          <el-form ref="form" :model="form" label-width="80px" :rules="rules">
            <el-form-item label="客户编号" prop="c_id">
              <el-input v-model="form.c_id"></el-input>
            </el-form-item>
            <el-form-item label="客户姓名">
              <el-input v-model="form.c_name" readonly></el-input>
            </el-form-item>
            <el-form-item label="身份证号">
              <el-input v-model="form.p_id" readonly></el-input>
            </el-form-item>
            <el-form-item label="性别">
              <el-input v-model="form.c_gender" readonly></el-input>
            </el-form-item>
            <el-form-item label="年龄">
              <el-input v-model="form.c_age" readonly></el-input>
            </el-form-item>
            <el-form-item label="手机号">
              <el-input v-model="form.c_telephone" readonly></el-input>
            </el-form-item>
            <el-form-item label="退住时间" prop="stay_back_date">
              <el-col>
                <el-date-picker type="date" placeholder="选择日期" v-model="form.stay_back_date" style="width: 100%;">
                </el-date-picker>
              </el-col>
            </el-form-item>
            <el-form-item label="退住类型" prop="stay_back_type">
              <el-select v-model="form.stay_back_type" placeholder="请选择退住类型">
                <el-option label="本人办理退住" value="本人办理退住"></el-option>
                <el-option label="家属办理退住" value="家属办理退住"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="退住原因" prop="stay_back_reason">
              <el-input type="textarea" v-model="form.stay_back_reason"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="insert('form')">保存</el-button>
              <el-button @click="cancel">取消</el-button>
            </el-form-item>
          </el-form>
        </el-dialog>
        <!-- 弹出框结束 -->
      </div>
      <!-- 入住表 -->
      <el-table :data="checkoutData" stripe style="width: 100%" height="542">
        <el-table-column prop="stay_back_id" label="退住编号" width="90">
        </el-table-column>
        <el-table-column prop="c_id" label="客户编号" width="90">
        </el-table-column>
        <el-table-column prop="c_name" label="客户姓名" width="90">
        </el-table-column>
        <el-table-column prop="p_id" label="身份号" width="170">
        </el-table-column>
        <el-table-column prop="c_gender" label="性别" width="80">
        </el-table-column>
        <el-table-column prop="c_age" label="年龄" width="80">
        </el-table-column>
        <el-table-column prop="c_telephone" label="电话" width="110">
        </el-table-column>
        <el-table-column prop="stay_back_date" label="退住时间" width="100">
        </el-table-column>
        <el-table-column prop="stay_back_type" label="退住类型" width="110">
        </el-table-column>
        <el-table-column prop="stay_back_reason" label="退住原因">
        </el-table-column>
        <el-table-column label="操作" width="220">
          <template slot-scope="scope">
            <el-button type="success" @click='update(scope.row)' size="small">编辑</el-button>
            <el-button v-if="$store.getters.getAdmin" type="danger" @click='deleteInfo(scope.row)' size="small">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
export default {
  name: "checkout.vue",
  data() {
    return {
      iscidExist: false, //当前cid是否存在
      checkoutData: [{
        c_age: 22,
        c_gender: "22",
        c_id: 2,
        c_name: "222",
        c_telephone: "",
        stay_back_date: null,
        stay_back_id: 3,
        stay_back_reason: "",
        stay_back_type: "",
        contacts: null,
        p_id: "222",
      }],
      dialogVisible: false,
      form: {
        c_id: '',
        c_name: '',
        p_id: '',
        c_gender: '',
        c_age: '',
        c_telephone: '',
        stay_back_date: '',
        stay_back_type: '',
        stay_back_reason: '',
      },
      rules: {
        c_id: [
          { required: true, message: '请输入客户编号', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              if (!this.iscidExist) {
                //console.log(this.iscidExist);
                callback(new Error('当前客户编号不存在'))
              } else {
                callback()
              }
            }, trigger: 'blur'
          }
        ],
        stay_back_date: { required: true, message: '请选择退住日期', trigger: 'blur' },
        stay_back_type: { required: true, message: '请选择退住类型', trigger: 'blur' },
      },
      dialogVisible: false,
      findName: '' //查询姓名
    }
  },
  methods: {
    findByName() {
      this.$axios({
        method: 'get',
        url: '/stay_back/find?name=' + this.findName,
        data: this.findName,
        //headers: { "Authorization": sessionStorage.getItem("token") }
      }).then((res) => {
        if (res.data.code === 200) {
          this.checkoutData = res.data.data
        } else {
          this.$message({
            type: 'error',
            message: '查询失败'
          });
        }
      })
    },
    insert(formname) {
      this.$refs[formname].validate((valid) => {
        if (valid) {
          console.log(this.form);
          this.$axios({
            method: 'post',
            url: '/stay_back/save',
            data: this.form,
            //headers: { "Authorization": sessionStorage.getItem("token") }
          }).then((res) => {
            console.log(res.data);
            if (res.data.code == 200 || res.data.code == 201) {
              //console.log("测试1" + res.data.data);
              this.$message({
                type: 'success',
                message: res.data.message
              });
              this.getList();
              this.form = {}
              // //console.log("测试2" + this.checkinData);
            }
            else {
              this.$message({
                type: 'error',
                message: res.data.message
              });
            }
            ////console.log(res.data)
            //this.checkinData = res.data.data
            //  //console.log(this.checkinData)
          })
          this.dialogVisible = false
        }
        else {  //校验不通过
          this.$message({
            type: 'error',
            message: '操作失败'
          });
        }
      });
    },
    update(row) {
      this.dialogVisible = true
      this.form = row
      console.log(this.form);
    },
    deleteInfo(row) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios({
          method: 'delete',
          url: '/stay_back/delete/' + row.stay_back_id,
          date: row.check_in_id,
          //headers: { "Authorization": sessionStorage.getItem("token") }
        }).then((res) => {
          this.$message({
            type: 'success',
            message: res.data.message
          });
          this.getList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    getList() {
      this.$axios({
        method: 'get',
        url: '/stay_back/list',
        //headers: { "Authorization": sessionStorage.getItem("token") }
      }).then((res) => {
        ////console.log(res.data)
        this.checkoutData = res.data.data
        ////console.log(this.checkinData[0])
      })
    },
    cancel() {
      this.getList()
      this.dialogVisible = false;
      // this.$refs[formName].resetFields();
      this.form = {}
    },
    closeDialog() {
      this.getList()
      this.dialogVisible = false;
      this.form = {}
      //this.$refs.form.resetFields();
    }
  },
  watch: {
    'form.c_id': {
      handler: function (newval, oldval) {
        if (newval && newval != '' && newval != undefined) {
          this.$axios({
            method: 'get',
            url: '/customer/findById/?id=' + this.form.c_id,
            //headers: { "Authorization": sessionStorage.getItem("token") }
          }).then((res) => {
            // //console.log(res.data)
            if (res.data.code === 200) {
              this.form.c_id = res.data.data.c_id
              this.form.c_name = res.data.data.c_name
              this.form.p_id = res.data.data.p_id
              this.form.c_gender = res.data.data.c_gender
              this.form.c_age = res.data.data.c_age
              this.form.c_telephone = res.data.data.c_telephone
              this.iscidExist = true
            } else {
              this.iscidExist = false
              this.form.c_name = ''
              this.form.c_gender = ''
              this.form.p_id = ''
              this.form.c_age = ''
            }
            ////console.log(this.checkinData[0])
          })
        }
        //do something
        // //console.log(this.form.c_id);
      },
    }
  },

  created() {
    this.getList();
  },
}
</script>

<style scoped>
</style>